﻿using Dapr.Actors;
using Dapr.Actors.Client;
using Dapr.Client;
using FEShop.AppUserService.Services.Dtos.Account;
using FEShop.AppUserService.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace FEShop.GatewayApi.Controllers.AppApi
{
    /// <summary>
    /// APP用户网关
    /// </summary>
    [ApiController]
    [Route("app/[controller]")]
    public class AppUserController : ControllerBase
    {
        private readonly ILogger<AppUserController> _logger;
        private readonly IActorProxyFactory _actorProxyFactory;

        /// <summary>
        /// 
        /// </summary>
        public AppUserController(ILogger<AppUserController> logger, IActorProxyFactory actorProxyFactory)
        {
            _logger = logger;
            _actorProxyFactory = actorProxyFactory;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("login")]
        public async Task<IActionResult> LoginAsync([FromBody] LoginInputDto dto)
        {
            ActorId actorId = new ActorId(dto.Account);
            IAccountService accountService =
                _actorProxyFactory.CreateActorProxy<IAccountService>(
                    actorId,
                    "AccountService");
            ResultObject<LoginOutputDto> result = await accountService.LoginAsync(dto);
            return Ok(result);
        }

        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("register")]
        public async Task<IActionResult> RegisterAsync([FromBody] RegisterInputDto dto)
        {
            ActorId actorId = new ActorId(dto.Account);
            IAccountService accountService =
                _actorProxyFactory.CreateActorProxy<IAccountService>(
                    actorId,
                    "AccountService");
            ResultObject<string> result = await accountService.RegisterAsync(dto);
            return Ok(result);
        }

    }
}
